package com.zhongxi.guoyuan.adapter.coinShop;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.coinStore.CoinStoreData;
import com.zhongxi.guoyuan.url.Url;

import java.util.List;

/**
 * Created by Administrator on 2016/10/29.
 */

public class CoinAdapter extends BaseAdapter {
    private List<CoinStoreData> data;
    private LayoutInflater mLayoutInflater;
    private Context context;

    public CoinAdapter(Context context, List<CoinStoreData> data) {
        this.mLayoutInflater = LayoutInflater.from(context);
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        try {
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mLayoutInflater.inflate(R.layout.adapter_coin, parent, false);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                holder.tvDesc = (TextView) convertView.findViewById(R.id.tvDesc);
                holder.tvView = (TextView) convertView.findViewById(R.id.tvView);
                holder.tvDistance = (TextView) convertView.findViewById(R.id.tvDistance);
                holder.rb = (RatingBar) convertView.findViewById(R.id.rb);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (null != data.get(position).getThumbnail() && !"".equals(data.get(position).getThumbnail())) {
//                Glide.with(context).load(Url.IMG_URL + data.get(position).getThumbnail()).placeholder(R.mipmap.iv_eg).transform(new GlideRoundTransform(context, 10)).into(holder.ivImg);
                Glide.with(context).load(Url.IMG_URL + data.get(position).getThumbnail()).placeholder(R.mipmap.iv_loading).fitCenter().into(holder.ivImg);
            }
            if (null != data.get(position).getName() && !"".equals(data.get(position).getName())) {
                holder.tvName.setText(data.get(position).getName());
            } else {
                holder.tvName.setText("");
            }
            if (null != data.get(position).getViewnum() && !"".equals(data.get(position).getViewnum())) {
                holder.tvView.setText(data.get(position).getViewnum());
            } else {
                holder.tvView.setText("");
            }
            if (null != data.get(position).getStarlevel() && !"".equals(data.get(position).getStarlevel())) {
                holder.rb.setRating(Float.parseFloat(data.get(position).getStarlevel()));
            } else {
                holder.rb.setRating(5f);
            }
            if (null != data.get(position).getAddress() && !"".equals(data.get(position).getAddress())) {
                holder.tvDesc.setText(data.get(position).getAddress());
            } else {
                holder.tvDesc.setText("");
            }
            if (null != data.get(position).getDistance() && !"".equals(data.get(position).getDistance())) {
                String dis = "";
                if (Float.parseFloat(data.get(position).getDistance()) / 1000 > 1) {
                    dis = Float.parseFloat(data.get(position).getDistance()) / 1000 + "Km";
                } else {
                    dis = data.get(position).getDistance() + "m";
                }
                holder.tvDistance.setText(dis);
            } else {
                holder.tvDistance.setText("");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }


    class ViewHolder{
        private TextView tvName;
        private TextView tvDesc;
        private TextView tvView;
        private TextView tvDistance;
        private RatingBar rb;
        private ImageView ivImg;
    }
}
